// pages/details/details.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgs:[
      "/images/banner01.jpg",
      "/images/banner02.jpg",
      "/images/banner03.jpg",
      "/images/banner04.jpg",
    ],
    isCollect:true,//收藏状态
    goods:{
      id:5,
      goodsImage:"/images/banner01.jpg",
      goodsName:"商品名称",
      goodsImgs:[
        "/images/banner01.jpg",
        "/images/banner02.jpg",
        "/images/banner03.jpg",
        "/images/banner04.jpg"
      ],
      goodsPrice:200,
      goodsPriceOld:300,
      goodsDetails:"/images/IMG_0466.JPG",
      isCollect:true
    },
  num:0,//商品数量
  },
  //收藏状态修改
setCollect(){
  this.setData({
    'goods.isCollect': !this.data.goods.isCollect
  })
},
//跳转购物车
goCar(){
    wx.switchTab({
      url: '/pages/car/car',
    })
},
goHome(){
  wx.switchTab({
    url: '/pages/home/home',
  })
},
// 加入购物车
setCarData(){
  //购物车数据
  let carLists = {
    id:this.data.goods.id,
    goodsName:this.data.goods.goodsName,
    goodsImage:this.data.goods.goodsImage,
    goodsPrice:this.data.goods.goodsPrice,
    num:1
  }

//数据添加购物车
//1.判断数据缓存是否存在
let goodsCarList = wx.getStorageSync('goodsCarList');//获取
if(goodsCarList){
  //2.有数据
  let index = goodsCarList.findIndex(item=>item.id == this.data.goods.id);
  if(index != -1){
    //2.1有相同数据，数量加1
    goodsCarList[index].num +=1;
  }
  else{
    //2.2没相同数据，添加数据
    goodsCarList.push(carLists)
  }
  //更新数据
  wx.setStorageSync('goodsCarList', goodsCarList);
}else{
  //3.没有数据，直接添加数据
  wx.setStorageSync('goodsCarList', [carLists]);
}
//显示商品数量
this.setData({
  num:wx.getStorageSync('goodsCarList').length
})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      console.log(options);
      let id = options.id;
      wx.request({
        url: 'http://47.115.51.185/api/product/detail/'+id,
        success:(res)=>{
          console.log(res)
          let {id,image,description,price,ot_price,slider_image,store_name} = res.data.data.storeInfo;
          this.setData({
            "goods.id":id,
            "goods.goodsImage":image,
            "goods.goodsName":store_name,
            "goods.goodsImgs":slider_image,
            "goods.goodsPrice":price,
            "goods.goodsPriceOld":ot_price,
            "goods.goodsDetails":description,
            "goods.isCollect":false,
          })
        }
      })
    },
    
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow:function() {
      //  显示商品数量
      this.setData({
        num:wx.getStorageSync('goodsCarList').length
      })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})